import Request from './request-fetch'
import message from '@utils/message'
import {getToken,setToken} from '@utils/store'

export const requestHeader = ()=>{
  return {
    "Accept":"application/json",
  }
}
export const transformResponse = (data,showMsg)=>{
  if(data.errorCode == 1){
     window.location.href=`/login/main`
  } else if(data.errorCode!=0){
    message.error(data.errorMsg)
  }else if((data.errorCode==0)&&showMsg){
    message.success(data.errorMsg||'成功')
  }
  return data
}

const req = new Request({
  // 接口响应数据，接口请求成功后，对接口数据进行处理，方法必须return处理后的数据
  transformResponse:transformResponse,
  // 接口请求前，接口请求前对headers请求头处理，比如接口需要的token参数
  requestHeader:requestHeader,
  // 接口响应头 ，接口请求成功后，对response.headers进行处理，比如读取headers里的token
  responseHeaders:(headers)=>{
    var token = headers.get('admin-token')
    if(token) setToken(token)
  },
  
})
export const get = req.get
export const post = req.post
export const postJson = req.json
export const form = req.form
export const download = req.download
export const uploadAndDownload = req.uploadAndDownload
export default {
  get:get,
  form:form,
  post:post,
  postJson:postJson,
  download:download,
  uploadAndDownload:uploadAndDownload,
}







